package com.xs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xs.common.R;
import com.xs.domain.Collect;
import com.xs.mapper.CollectMapper;
import com.xs.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author admin
* @description 针对表【collect(收集表)】的数据库操作Service实现
* @createDate 2023-03-03 13:46:23
*/
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
implements CollectService{
    @Autowired
    private CollectMapper collectMapper;

    /**
     * 新增收藏
     * @param collect
     * @return
     */
    @Override
    public R insertCollect(Collect collect) {
        if (collect.getStaffId() == null || collect.getStaffId().equals("")) {
            return R.error(400, "收藏歌曲为空");
        }
        if (collectMapper.existConsumer(collect.getConsumerId(), collect.getStaffId()) > 0) {
            return R.error(300, "已收藏");
        }
        int a = collectMapper.insert(collect);
        if (a > 0) {
            return R.success("收藏成功");
        } else {
            return R.error(400, "收藏失败");
        }

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public R deleteCollect(Integer id) {
        if (collectMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error(400, "删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public R deleteBathIds(List<Integer> ids) {
        System.out.println("ids = " + ids);
        int i = collectMapper.deleteBatchIds(ids);
        if (i < 0) {
            return R.error(400,"批量删除失败");
        }
        return R.success("批量删除成功");
    }

    /**
     * 查询所有收藏
     * @return
     */
    @Override
    public R allCollect() {
        if ( collectMapper.selectList(null)!=null) {
            return R.success("查找成功");
        } else {
            return R.error(400,"查找失败");
        }
    }

    /**
     * 查询某个用户的收藏列表
     * @param consumerId
     * @return
     */
    @Override
    public R collectOfConsumerId(Integer consumerId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumer_id",consumerId);
        List<Collect> collect = collectMapper.selectList(queryWrapper);
        return R.success("查询成功",collect);
    }

    /**
     * 查询某个用户是否收藏某个乐谱
     * @param consumerId,staffId
     * @param staffId
     * @return
     */
    @Override
    public boolean existConsumer(Integer consumerId, Integer staffId) {
        return collectMapper.existConsumer(consumerId,staffId)>0;
    }

    /**
     * 判断用户是否收藏该乐谱
     * @param consumerId
     * @param staffId
     * @return
     */
    @Override
    public R isCollectOfStaff(Integer consumerId, Integer staffId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumer_id", consumerId);
        queryWrapper.eq("staff_id", staffId);
        Collect collect = collectMapper.selectOne(queryWrapper);
        return R.success("查询成功", collect);
    }

}
